package com.ld.tamp.web.listener;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.ld.base.support.PropertyHolder;
import com.ld.tamp.web.bean.AuthorizerInfo;
import com.ld.tamp.web.bean.AuthorizerJsTicket;
import com.ld.tamp.web.bean.ComponentAccessToken;
import com.ld.tamp.web.constant.WeiXinConstant;
import com.ld.tamp.web.dao.AuthorizerInfoDao;
import com.ld.tamp.web.dao.AuthorizerJsTicketDao;
import com.ld.tamp.web.dao.ComponentAccessTokenDao;
import com.ld.tamp.web.dao.ComponentVerifyTicketDao;
import com.ld.tamp.web.support.AuthorizerAccessTokenManager;
import com.ld.tamp.web.support.AuthorizerJsTicketManager;
import com.ld.tamp.web.support.ComponentTokenManager;

import weixin.popular.api.ComponentAPI;

/**
 * @author KeYang
 *
 */
public class ComponentTokenManagerListener implements ApplicationListener<ContextRefreshedEvent> {

	private static final Logger logger = LoggerFactory.getLogger(ComponentTokenManagerListener.class);

	@Autowired
	private ComponentAccessTokenDao componentAccessTokenDao;

	@Autowired
	private ComponentVerifyTicketDao componentVerifyTicketDao;
	
	@Autowired
	private AuthorizerInfoDao authorizerInfoDao;
	
	@Autowired
	private AuthorizerJsTicketDao authorizerJsTicketDao;
	

	/*
	 * (non-Javadoc)
	 * 程序启动时监听
	 * @see
	 * org.springframework.context.ApplicationListener#onApplicationEvent(org.
	 * springframework.context.ApplicationEvent)
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {

		String componentAppid = PropertyHolder.getProperty(WeiXinConstant.COMPONENT_APPID);
		String componentAppsecret = PropertyHolder.getProperty(WeiXinConstant.COMPONENT_APPSECRET);

		String componentVerifyTicket = componentVerifyTicketDao.read();
		ComponentAccessToken componentAccessToken = componentAccessTokenDao.read();
		Map<String, AuthorizerInfo> authorizerInfoMap = authorizerInfoDao.readEntries();
		Map<String, AuthorizerJsTicket> authorizerJsTicketMap = authorizerJsTicketDao.readEntries();
		long passedTime;
		if(authorizerInfoMap!=null){
			Iterator<Entry<String, AuthorizerInfo>> iterator = authorizerInfoMap.entrySet().iterator();
			while(iterator.hasNext()){
				Entry<String, AuthorizerInfo> entry = iterator.next();
				AuthorizerInfo authorizerInfo = entry.getValue();
				AuthorizerAccessTokenManager.init(authorizerInfo.getAuthorizer_appid(), authorizerInfo.getAuthorizer_refresh_token(), 0, componentAccessTokenDao, authorizerInfoDao,authorizerJsTicketDao);
			}
		}
		
		if (componentAccessToken != null) {
			passedTime = componentAccessToken.getExpiresTime() - System.currentTimeMillis();
			if (passedTime > 0) {
				int initialDelay = (int) Math.floor(passedTime / 1000);
				ComponentTokenManager.init(componentAppid, componentAppsecret, initialDelay,
						componentAccessTokenDao, componentVerifyTicketDao);
				return;
			}
		}
		
		if (componentVerifyTicket != null) {
			weixin.popular.bean.component.ComponentAccessToken newComponentAccessToken = ComponentAPI
					.api_component_token(componentAppid, componentAppsecret, componentVerifyTicket);
			if (!newComponentAccessToken.isSuccess()) {
				logger.info("启动时获取component_access_token出错,error_code:{},error_msg:{}",
						newComponentAccessToken.getErrcode(), newComponentAccessToken.getErrmsg());
			} else {
				componentAccessToken.setComponentAccessToken(newComponentAccessToken.getComponent_access_token());
				componentAccessToken
						.setExpiresTime(newComponentAccessToken.getExpires_in() * 1000 + System.currentTimeMillis());
				componentAccessTokenDao.save(componentAccessToken);
				ComponentTokenManager.init(componentAppid, componentAppsecret, 118, componentAccessTokenDao,
						componentVerifyTicketDao);
			}
		}
		
	}
	
	
}
